<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

use app\common\model\UserAccount;
use app\common\model\UserRankingData;
use think\Db;

/**
 * Action逻辑
 */
class Action extends LogicBase
{
    

    
    /**
     * 获取Action列表
     */
    public function getActionList($where = [], $field = 'a.*', $order = '')
    {
        

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];

        $count=$this->modelGamblerKingAction->alias('a')->join('__GAMBLER_KING__ k','k.id=a.king_id')->where($where)->count();
        $list=$this->modelGamblerKingAction->alias('a')->field($field)->join('__GAMBLER_KING__ k','k.id=a.king_id')->where($where)->order($order)->paginate(DB_LIST_ROWS);
        $this->modelGamblerKingAction->alias('a');
        return ['list'=>$list,'count'=>$count] ;
    }
    
    /**
     * 获取Action列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['a.content|a.name'] = ['like', '%'.$data['search_data'].'%'];
        !empty($data['type']) && $where['a.type']=$data['type'];
        return $where;
    }
    
    /**
     * Action信息编辑
     */
    public function ActionEdit($data = [])
    {
        

        
        $url = url('ActionList');

        if(empty($data['king_id'])){
            return [RESULT_ERROR, '请选择赌王'];
        }
        if(empty($data['content'])){
            return [RESULT_ERROR, '动态不能为空'];
        }
        $datas=[];
        switch ($data['type']){
            case 1:
            case 2:
            case 4:
                $result = $this->modelGamblerKingAction->setInfo($data);
                break;
            case 3:
                foreach ($data['username'] as $k=>$v){
                    $userinfo=\app\common\model\Member::get(['username'=>$v]);
                    if(!$userinfo){
                        return [RESULT_ERROR, $v.' 用户不存在'];
                    }
                    $arr=[
                        'username'=>$v,
                        'money'=>$data['recharge'][$k],
                        'bonus'=>$data['profit'][$k],
                        'king_bonus'=>$data['entrust'][$k],
                        'winn_count'=>$data['withdrwal'][$k],
                        'draw_count'=>$data['total_price'][$k],
                        'lose_count'=>$data['poundage_number'][$k],
                    ];
                    $datas[]=$arr;
                }
                $data['content_json']=json_encode($datas);
                $result = $this->modelGamblerKingAction->setInfo($data);

                break;
        }


        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, 'Action' . $handle_text.'赌王动态');
        
        return $result ? [RESULT_SUCCESS, 'Action操作成功', $url] : [RESULT_ERROR, $this->modelGamblerKingAction->getError()];
    }

    /**
     * 获取Action信息
     */
    public function getActionInfo($where = [], $field = 'a.*')
    {
        
        $this->modelGamblerKingAction->alias('a');
        

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        return $this->modelGamblerKingAction->getInfo($where, $field);
    }
    

    /***
     *执行模板
     */

    public function implement($data)
    {
       $info=$this->modelGamblerKingAction->getInfo(['id'=>$data['id']]);
       if(!$info){
            return [RESULT_ERROR, '模板不存在'];
       }
        $arrs=[];
        $content=$info['content'];

       switch ($info['type']){
           case 1:
                //生成用户数据


               foreach ($content as $k=>$v){
                  if($v['recharge']){
                    $arr=[
                        'type'=>1,
                        'user_id'=>$v['user_id'],
                        'remark'=>'模板生成',
                        'create_time'=>TIME_NOW,
                        'money'=>$v['recharge'],
                        'is_user'=>1
                    ];
                    $arrs[]=$arr;
                  }
                   if($v['profit']){
                       $arr=[
                           'type'=>2,
                           'user_id'=>$v['user_id'],
                           'remark'=>'模板生成',
                           'create_time'=>TIME_NOW,
                           'money'=>$v['profit'],
                           'is_user'=>1
                       ];
                       $arrs[]=$arr;
                   }
                   if($v['entrust']){
                       $arr=[
                           'type'=>3,
                           'user_id'=>$v['user_id'],
                           'remark'=>'模板生成',
                           'create_time'=>TIME_NOW,
                           'money'=>$v['entrust'],
                           'is_user'=>1
                       ];
                       $arrs[]=$arr;
                   }
                   if($v['withdrwal']){
                       $arr=[
                           'type'=>4,
                           'user_id'=>$v['user_id'],
                           'remark'=>'模板生成',
                           'create_time'=>TIME_NOW,
                           'money'=>$v['withdrwal'],
                           'is_user'=>1
                       ];
                       $arrs[]=$arr;
                   }
               }
               break;
           case 2:
               foreach ($content as $k=>$v){
                   if($v['profit']){
                       $arr=[
                           'type'=>5,
                           'user_id'=>$v['user_id'],
                           'king_id'=>$v['king_id'],
                           'remark'=>'模板生成',
                           'create_time'=>TIME_NOW,
                           'money'=>$v['profit'],
                       ];
                       $arrs[]=$arr;
                   }
                   if($v['bonus']){
                       $arr=[
                           'type'=>6,
                           'user_id'=>$v['user_id'],
                           'king_id'=>$v['king_id'],
                           'remark'=>'模板生成',
                           'create_time'=>TIME_NOW,
                           'money'=>$v['bonus'],

                       ];
                       $arrs[]=$arr;
                   }
                   if($v['bets']){
                       $arr=[
                           'type'=>7,
                           'user_id'=>$v['user_id'],
                           'king_id'=>$v['king_id'],
                           'remark'=>'模板生成',
                           'create_time'=>TIME_NOW,
                           'money'=>$v['bets'],

                       ];
                       $arrs[]=$arr;
                   }
                   if($v['entrust']){
                       $arr=[
                           'type'=>8,
                           'user_id'=>$v['user_id'],
                           'king_id'=>$v['king_id'],
                           'remark'=>'模板生成',
                           'create_time'=>TIME_NOW,
                           'money'=>$v['entrust'],

                       ];
                       $arrs[]=$arr;
                   }
                   if($v['credit']){
                       $arr=[
                           'type'=>9,
                           'user_id'=>$v['user_id'],
                           'king_id'=>$v['king_id'],
                           'remark'=>'模板生成',
                           'create_time'=>TIME_NOW,
                           'money'=>$v['credit'],

                       ];
                       $arrs[]=$arr;
                   }
               }

               break;
           case 3:
               foreach ($content as $k=>$v){
                   if($v['success_order_count']){
                       $arr=[
                           'type'=>10,
                           'user_id'=>$v['user_id'],
                           'shop_id'=>$v['shop_id'],
                           'remark'=>'模板生成',
                           'create_time'=>TIME_NOW,
                           'money'=>$v['success_order_count'],
                       ];
                       $arrs[]=$arr;
                   }
                   if($v['order_count']){
                       $arr=[
                           'type'=>11,
                           'user_id'=>$v['user_id'],
                           'shop_id'=>$v['shop_id'],
                           'remark'=>'模板生成',
                           'create_time'=>TIME_NOW,
                           'money'=>$v['order_count'],

                       ];
                       $arrs[]=$arr;
                   }
                   if($v['order_total']){
                       $arr=[
                           'type'=>12,
                           'user_id'=>$v['user_id'],
                           'shop_id'=>$v['shop_id'],
                           'remark'=>'模板生成',
                           'create_time'=>TIME_NOW,
                           'money'=>$v['order_total'],

                       ];
                       $arrs[]=$arr;
                   }
                   if($v['profit']){
                       $arr=[
                           'type'=>13,
                           'user_id'=>$v['user_id'],
                           'shop_id'=>$v['shop_id'],
                           'remark'=>'模板生成',
                           'create_time'=>TIME_NOW,
                           'money'=>$v['profit'],

                       ];
                       $arrs[]=$arr;
                   }
                   if($v['total_price']){
                       $arr=[
                           'type'=>14,
                           'user_id'=>$v['user_id'],
                           'king_id'=>$v['shop_id'],
                           'remark'=>'模板生成',
                           'create_time'=>TIME_NOW,
                           'money'=>$v['total_price'],

                       ];
                       $arrs[]=$arr;
                   }
                   if($v['poundage_number']){
                       $arr=[
                           'type'=>15,
                           'user_id'=>$v['user_id'],
                           'king_id'=>$v['shop_id'],
                           'remark'=>'模板生成',
                           'create_time'=>TIME_NOW,
                           'money'=>$v['poundage_number'],

                       ];
                       $arrs[]=$arr;
                   }
               }

               break;
       }
       Db::startTrans();
       try{
           UserRankingData::insertall($arrs);
           $info->use_count+=1;
           $info->save();
           Db::commit();
       }catch (\Exception $e){
           Db::rollback();
           return [RESULT_ERROR,$e->getMessage()];
       }
       return  [RESULT_SUCCESS, '执行成功'];


    }


}
